package UIFree.controls
{
	import UIFInterface.ILabel;
	
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	import UIFree.uif_fonts;
	
	import UIUtils.uif_util;
	
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class uif_label extends uif_ecomponent implements ILabel {
		private var _textField: TextField;
		public function uif_label(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass: Object='') {
			_textField = new TextField();
			super(parent, ax, ay, awidth, aheight, styleClass);
			addChild(_textField);
		}
		
		override protected function applyStyle(a_style: Object): void {
			super.applyStyle(a_style);
			if (style.hasOwnProperty('text_format')) {
				applyTextFormat(style['text_format']);
			} else {
				_textField.selectable = false;
			}
		}
		
		public function applyTextFormat(tfStyle: Object): void {
			uif_util.applyTextFormat(_textField, tfStyle);
			if (style.embedFonts) uif_fonts.applyFont(tfStyle.font, _textField, tfStyle.size, tfStyle.color);
		}
		
		public function appendText(aText: String): void {
			_textField.appendText(aText);
		}
		
/*		public function appendHTMLText(aText: String): void {
			htmlText = htmlText + aText;
		}
	
		*/
		public function get textField(): TextField {
			return _textField;
		}
		
		public function set text(aText: String): void {
			if (aText) {
				_textField.text = aText.toString();
				updateTextFieldBounds();
			} else _textField.text = '';
		}
		
		public function get htmlText(): String {
			return _textField.htmlText;
		}
		
		public function set htmlText(aText: String): void {
			if (aText) {
				_textField.htmlText = aText.toString();
				updateTextFieldBounds();
			} else _textField.htmlText = '';
		}
		
		protected function updateVAlign(): void {
			var sh: int = height - _textField.textHeight;
			if (style.valign) _textField.y = sh / 2 - 3;			
		}
		
		public function get text(): String {
			return _textField.text;
		}
		
		protected function updateTextFieldBounds(): void {
			var inner: Rectangle = this.getInner();
			_textField.x = inner.x;
			_textField.y = inner.y;
			_textField.width  = inner.width;
			if (_textField.textHeight <= height)
				_textField.height = height;
			else _textField.height = height;
			updateVAlign();
		}
		
		override protected function updateBounds(): uif_component {
			updateTextFieldBounds();
			return super.updateBounds();
		}
	}
}